<template>
  <div class="app-container flex-cloumn">
    <el-row :gutter="12"  class="app-container_div">
      <el-col :span="14" class="wrapper-box">
          <el-form :model="queryCpAccountParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
            <el-row :gutter="1" class="mb8">
              <el-col :offset="14" :span="6">
                <el-form-item label="交易对手" prop="partyName">
                  <el-input
                      v-model="queryCpAccountParams.partyName"
                      clearable
                      size="mini"
                      style="width: 150px"
                  >
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="1.5">
                <el-form-item>
                  <el-button type="primary" icon="Search" size="mini" @click="handleCpAccountQuery">搜索</el-button>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <xTable ref="refCpAccountTable" v-loading="loading" :data="cpAccountList" multiple="false"
                  refId="refCpAccountTable" @selection-change="handleUserSelectionChange" class="tableDragger">
            <template #columList>
              <el-table-column type="selection" width="55" align="center"/>
              <el-table-column label="序号" type="index" width="55"/>
              <el-table-column label="交易对手名称" align="left" prop="partyName"/>
              <el-table-column label="状态" align="center" prop="status" v-if="true">
                <template #default="scope">
                <span>{{
                    scope.row.status == -1 ? "审批" : (scope.row.status == 0 ? "新建" : (scope.row.status == 1 ? "启用" : "停用"))
                  }}</span>
                </template>
              </el-table-column>
            </template>
          </xTable>
          <pagination

              v-show="cpAccountTotal>0"
              :total="cpAccountTotal"
              v-model:page="queryCpAccountParams.pageNum"
              v-model:limit="queryCpAccountParams.pageSize"
              style="float:right"
              @pagination="getCpAccountLists"
          />
      </el-col>
      <el-divider direction="vertical" content-position="center"></el-divider>
      <el-col :span="8">
        <div class="app-container">
          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button
                  type="primary"
                  plain
                  icon="Plus"
                  size="mini"
                  :disabled="single"
                  @click="handleAdd"
                  v-hasPermi="['system:dict:add']"
              >新增
              </el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                  type="success"
                  plain
                  icon="Edit"
                  size="mini"
                  :disabled="single2"
                  @click="handleUpdate"
                  v-hasPermi="['system:dict:edit']"
              >修改
              </el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                  type="danger"
                  plain
                  icon="Delete"
                  size="mini"
                  :disabled="single2"
                  @click="handleDelete"
                  v-hasPermi="['system:dict:remove']"
              >删除
              </el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                  type="danger"
                  icon="Plus"
                  size="mini"
                  :disabled="single3"
                  @click="handleStop"
                  v-hasPermi="['system:dict:stop']"
              >停用
              </el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                  type="success"
                  icon="Plus"
                  size="mini"
                  :disabled="single4"
                  @click="handleStart"
                  v-hasPermi="['system:dict:start']"
              >启用
              </el-button>
            </el-col>
          </el-row>
          <xTable ref="refCpAccountUserTable" v-loading="loading2" :data="cpAccountUserList" multiple="false"
                  refId="refCpAccountUserTable" @selection-change="handleAccountUserSelectionChange">
            <template #columList>
              <el-table-column type="selection" width="55" align="center"/>
              <el-table-column label="序号" type="index" width="55"/>
              <el-table-column label="账户名称" align="center" prop="accName"/>
              <el-table-column label="创建者" align="center" prop="creater"/>
              <el-table-column label="状态" align="center" prop="status">
                <template #default="scope">
                <span>{{
                    scope.row.status == 3 ? "审批中" : (scope.row.status == 0 ? "新建" : (scope.row.status == 1 ? "启用" : "停用"))
                  }}</span>
                </template>
              </el-table-column>
            </template>
          </xTable>
          <pagination
              v-show="cpAccountUserTotal>0"
              :total="cpAccountUserTotal"
              v-model:page="queryUserParams.pageNum"
              v-model:limit="queryUserParams.pageSize"
              style="float:right"
              @pagination="getCpAccountUserList"
          />
        </div>
      </el-col>
    </el-row>
    <!--添加账号对话框-->
    <el-dialog :title="priceTitle" v-model="cpAccountAddOpen" width="500px" append-to-body>
      <el-form ref="accountFromRef" :model="cpAccountAddForm" :rules="cpAccountRules" label-width="110px">
        <el-form-item label="交易对手" prop="partyName">
          <el-input v-model="cpAccountAddForm.partyName" disabled/>
        </el-form-item>
        <el-form-item label="账户名称" prop="accName">
          <el-input v-model="cpAccountAddForm.accName"/>
        </el-form-item>
        <el-form-item label="状态">
          <el-input placeholder="新建" disabled>
          </el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancelAdd">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <!-- 修改账户对话框 -->
    <el-dialog :title="priceTitle" v-model="cpAccountUpdateOpen" width="500px" append-to-body>
      <el-form ref="accountFromRef" :model="cpAccountUpdateForm" :rules="cpAccountRules" label-width="110px">
        <el-form-item label="交易对手" prop="partyName">
          <el-input v-model="cpAccountUpdateForm.partyName" disabled/>
        </el-form-item>
        <template v-if="cpAccountUpdateForm.accId!=null">
          <el-form-item label="账户编号" prop="accId">
            <el-input v-model="cpAccountUpdateForm.accId" disabled/>
          </el-form-item>
        </template>
        <el-form-item label="账户名称" prop="accName">
          <el-input v-model="cpAccountUpdateForm.accName"/>
        </el-form-item>
        <el-form-item label="状态">
          <template v-if="cpAccountUpdateForm.status==0">
            <el-input disabled placeholder="新建"/>
          </template>
          <template v-if="cpAccountUpdateForm.status==1">
            <el-input disabled placeholder="启用"/>
          </template>
          <template v-if="cpAccountUpdateForm.status==2">
            <el-input disabled placeholder="停用"/>
          </template>
          <template v-if="cpAccountUpdateForm.status==3">
            <el-input disabled placeholder="审批中"/>
          </template>
        </el-form-item>
        <template v-if="cpAccountUpdateForm.creater!=null">
          <el-form-item label="创建者">
            <el-input disabled v-model="cpAccountUpdateForm.creater"/>
          </el-form-item>
        </template>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancelUpdate">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="cpaccount">
import {ref} from "vue";
import {
  getCpAccountList,
  queryAccountByPartyId,
  saveAccount,
  updateAccount,
  deleteAccount,
} from '@/api/account/cpaccountmanagement'

const {proxy} = getCurrentInstance();
const {M_TYPE, A_TYPE, CURRENCY, productType} = proxy.useDict("M_TYPE", "A_TYPE", "CURRENCY", "productType");
import xTable from "@/xquant/base/xTable";
// 用户组列表
const cpAccountList = ref([]);
// 用户组列表
const cpAccountUserList = ref([]);
const cpAccountUpdateOpen = ref(false);
const cpAccountAddOpen = ref(false);
const templateSelection = ref({});

const priceTitle = ref('');

const loading = ref(true);
const loading2 = ref(false);
const showSearch = ref(true);
const showSearch2 = ref(true);
const radioSelect = ref('');
const disableFlag = ref(false);
//
const ids = ref([]);
const ids2 = ref([]);
const names = ref([]);
const names2 = ref([]);
//索引
const indexs = ref([]);
const indexs2 = ref([]);
//数据
const rows = ref([]);
const rows2 = ref([]);

const single = ref(true);
const single3 = ref(true);
const single4 = ref(true);
const multiple = ref(true);
const single2 = ref(true);
const multiple2 = ref(true);
const cpAccountTotal = ref(0);
const cpAccountUserTotal = ref(0);
const selectionAccountRow = ref([]);
const selectionUserGroupRow = ref([]);
const action = ref('');

const data = reactive({
  cpAccountRules: {
    accName: [{required: true, message: "账户名称不能为空", trigger: "blur"}]
  },
  cpAccountForm: {
    partyId: null,
    accName: null,
    partyName: null,
    status: null,
    accRight: null,
  },
  queryCpAccountParams: {
    pageNum: 1,
    pageSize: 20,
    status: 1,
    partyId: null,
    partyName: null,
  },
  cpAccountUpdateForm: {
    accId: null,
    partyName: null,
    accName: null,
    status: '0',
    creater: null,
  },
  cpAccountAddForm: {
    partyId: null,
    partyName: null,
    accName: null,
    status: '0',
  },
  queryUserParams: {
    pageNum: 1,
    pageSize: 20,
    partyId: null,
  },
});
const {
  cpAccountAddForm,
  queryUserParams,
  cpAccountUpdateForm,
  queryCpAccountParams,
  cpAccountForm,
  cpAccountRules,
} = toRefs(data);


/** 查询按钮 */
function handleCpAccountQuery() {
  getCpAccountLists();
}

/** 查询账户列表 */
function getCpAccountLists() {
  loading.value = true;
  getCpAccountList(queryCpAccountParams.value).then(response => {
    cpAccountList.value = response.list;
    cpAccountTotal.value = response.total;
    loading.value = false;
  });
}

/**账户 新增按钮操作 */
function handleAdd() {
  reset();
  disableFlag.value = false;
  cpAccountAddOpen.value = true;
  priceTitle.value = "新增账户";
  cpAccountAddForm.value.status = 0;
  action.value = 'add';
}

/**
 *
 * 账户  修改按钮操作
 * */
function handleUpdate() {
  reset();
  action.value = 'update';
  disableFlag.value = false;
  cpAccountUpdateOpen.value = true;
  priceTitle.value = "修改账户信息";
  cpAccountUpdateForm.value.accId = rows2.value[0].accId;
  cpAccountUpdateForm.value.accName = rows2.value[0].accName;
  cpAccountUpdateForm.value.status = rows2.value[0].status;
  cpAccountUpdateForm.value.creater = rows2.value[0].creater;
  cpAccountUpdateForm.value.partyName = rows.value[0].partyName;
}

/**
 *
 * 修改/新增   保存操作
 * */
function submitForm() {
  proxy.$refs["accountFromRef"].validate(valid => {
    if (valid) {
      if (action.value == 'update') {
        updateAccount(cpAccountUpdateForm.value).then(response => {
          if (response.success) {
            proxy.$modal.msgSuccess("修改成功");
            cpAccountUpdateOpen.value = false;
            getCpAccountUserList();
          } else {
            proxy.$modal.msgError(response.msg);
          }
        });
      } else {
        saveAccount(cpAccountAddForm.value).then(response => {
          if (response.success) {
            proxy.$modal.msgSuccess("新增成功");
            cpAccountAddOpen.value = false;
            getCpAccountUserList();
          } else {
            proxy.$modal.msgError(response.msg);
          }
        });
      }
    }
  });
}

function getCpAccountUserList() {
  loading2.value = true;
  queryUserParams.value.partyId = rows.value[0].partyId;
  queryAccountByPartyId(queryUserParams.value).then(response => {
    cpAccountUserList.value = response.list;
    cpAccountUserTotal.value = response.total;
    loading2.value = false;
  });
}

function handleUserSelectionChange(selection) {
  if (selection.length == 0) {
    ids.value = [];
    indexs.value = [];
  } else {
    ids.value = selection.map(item => item.partyId);
    names.value = selection.map(item => item.partyName);
    indexs.value = selection.map(item => item.index);
    rows.value = selection.map(item => item);
  }
  single.value = selection.length != 1;
  multiple.value = !selection.length;
  if (selection.length == 1) {
    queryCpAccountParams.value.partyId = ids.value[0];
    cpAccountAddForm.value.partyName = names.value[0];
    cpAccountAddForm.value.partyId = ids.value[0];
    getCpAccountUserList();
    return;
  }
  if (selection.length != 1) {
    cpAccountUserList.value = [];
  }
}

/**
 *用户组
 *  多选框  选中数据
 * */
function handleAccountUserSelectionChange(selection) {
  if (selection.length == 0) {
    ids2.value = [];
    indexs2.value = [];
    names2.value = [];
  } else {
    ids2.value = selection.map(item => item.accId);
    indexs2.value = selection.map(item => item.index);
    rows2.value = selection.map(item => item);
    names2.value = selection.map(item => item.accName);
  }
  single2.value = selection.length != 1;
  multiple2.value = !selection.length;
  if (selection.length == 1) {
    queryCpAccountParams.value.partyId = ids.value[0];
    if (rows2.value[0].status === 1) {
      single3.value = false;
    }
    if (rows2.value[0].status === 2 || rows2.value[0].status === 0) {
      single4.value = false;
    }
  }
  if (selection.length != 1) {
    single3.value = true;
    single4.value = true;
  }
}

/** 删除按钮操作 */
function handleDelete() {
  const accNames = names2.value[0];
  var params = {
    accId: rows2.value[0].accId
  };
  proxy.$modal.confirm('是否确认对手账户名称为 "' + accNames + '"的数据项？').then(function () {
    return deleteAccount(params);
  }).then(() => {
    getCpAccountUserList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
    proxy.$modal.msgSuccess("删除失败");
  });
}


/** 取消修改按钮 */
function cancelUpdate() {
  cpAccountUpdateOpen.value = false;
  reset();
}

/** 取消添加按钮 */
function cancelAdd() {
  cpAccountAddOpen.value = false;
  reset();
}

/** 表单重置 */
function reset() {
  cpAccountUpdateForm.value = {
    accId: null,
    accName: null,
    status: null,
    creater: null
  };
  proxy.resetForm("accountFromRef");
}

function rowClick(row, column, event) {
  let refsElTable = proxy.$refs.refCpAccountTable; // 获取表格对象
  let findRow = selectionAccountRow.value.find(c => c.rowIndex == row.rowIndex);
  if (findRow) {
    refsElTable.toggleRowSelection(row, false);
    return;
  }
  refsElTable.clearSelection();
  refsElTable.toggleRowSelection(row); // 调用选中行方法
};

function rowStyle({row, rowIndex}) {
  Object.defineProperty(row, 'rowIndex', { //给每一行添加不可枚举属性rowIndex来标识当前行
    value: rowIndex,
    writable: true,
    enumerable: false
  })
};

function rowClassName({row, rowIndex}) {
  //把每一行的索引放进row
  row.index = rowIndex;
  let rowName = "",
      findRow = selectionAccountRow.value.find(c => c.rowIndex === row.rowIndex);
  if (findRow) {
    rowName = "current-row"; // elementUI 默认高亮行的class类 不用再样式了^-^,也可通过css覆盖改变背景颜色
  }
  return rowName; //也可以再加上其他类名 如果有需求的话
};

function rowClick2(row, column, event) {
  let refsElTable = proxy.$refs.refCpAccountUserTable; // 获取表格对象
  let findRow = selectionUserGroupRow.value.find(c => c.rowIndex == row.rowIndex);
  if (findRow) {
    refsElTable.toggleRowSelection(row, false);
    return;
  }
  refsElTable.clearSelection();
  refsElTable.toggleRowSelection(row); // 调用选中行方法
};

function rowStyle2({row, rowIndex}) {
  Object.defineProperty(row, 'rowIndex', { //给每一行添加不可枚举属性rowIndex来标识当前行
    value: rowIndex,
    writable: true,
    enumerable: false
  })
};

function rowClassName2({row, rowIndex}) {
  //把每一行的索引放进row
  row.index = rowIndex;
  let rowName = "",
      findRow = selectionUserGroupRow.value.find(c => c.rowIndex === row.rowIndex);
  if (findRow) {
    rowName = "current-row"; // elementUI 默认高亮行的class类 不用再样式了^-^,也可通过css覆盖改变背景颜色
  }
  return rowName; //也可以再加上其他类名 如果有需求的话
};

// 停用
function handleStop() {
  cpAccountUpdateForm.value.accId = rows2.value[0].accId;
  cpAccountUpdateForm.value.accName = rows2.value[0].accName;
  cpAccountUpdateForm.value.status = 2;
  updateAccount(cpAccountUpdateForm.value).then(response => {
    if (response.success) {
      proxy.$modal.msgSuccess("修改成功");
      single3.value = true;
      reset();
      getCpAccountUserList();
    } else {
      single3.value = true;
      proxy.$modal.msgError(response.msg);
    }
  });
}

// 启用
function handleStart() {
  cpAccountUpdateForm.value.accId = rows2.value[0].accId;
  cpAccountUpdateForm.value.accName = rows2.value[0].accName;
  cpAccountUpdateForm.value.status = 1;
  updateAccount(cpAccountUpdateForm.value).then(response => {
    if (response.success) {
      proxy.$modal.msgSuccess("修改成功");
      single4.value = true;
      reset();
      getCpAccountUserList();
    } else {
      single4.value = true;
      proxy.$modal.msgError(response.msg);
    }
  });
}

getCpAccountLists();
</script>
